import com.sun.scenario.effect.Merge;

import java.lang.reflect.Member;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Test09 {
    public static int MLS (int[] arr) {
        // write code here
        arr =  mergeSort(arr,0,arr.length-1);
        int prev = 0;
        int cur = 0;
        int curnext=0;
        int max = 0;
        int  record = 0;
        while(cur<arr.length){
            curnext = cur+1;
            if(curnext>=arr.length){
                break;
            }
            if((arr[cur]+1)==arr[curnext]){
                cur=curnext;
            }else if(arr[cur]==arr[curnext]){
                record++;
                cur = curnext;
            }
            else{
                max = Math.max(max,(cur-prev+1-record));
                record=0;
                prev=curnext;
                cur = curnext;
            }
        }
        if(max==0){
            return 1;
        }
        return max;

    }


    public static int[] mergeSort(int[] arr,int left,int right){

        if(left==right){
            return new int[]{arr[left]};
        }
        int mid = left+(right-left)/2;
        int[] leftArr = mergeSort(arr,left,mid);
        int[] rightArr =mergeSort(arr,mid+1,right);
        int[] newArr = hebing(leftArr,rightArr);
        return newArr;
    }

    public static int[] hebing(int[] leftArr,int[] rightArr){
        int[] tmp = new int[leftArr.length+rightArr.length];
        int n = 0;
        int m = 0;
        int k =0;
        while(n<leftArr.length&&m<rightArr.length){
            if(leftArr[n]<rightArr[m]){
                tmp[k] = leftArr[n];
                n++;
                k++;
            }else{
                tmp[k] = rightArr[m];
                m++;
                k++;
            }
        }
        if(n<leftArr.length){
            while(n<leftArr.length){
                tmp[k] = leftArr[n];
                k++;
                n++;
            }
        }
        if(m<rightArr.length){
            while(m<rightArr.length){
                tmp[k] = rightArr[m];
                k++;
                m++;
            }
        }
        return tmp;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{2,2,2,3,1,5};
      int a = MLS(arr);
        System.out.println(a);

    }
    public static void main1(String[] args) {
        int[] arr = new int[]{9,8,6,5,3,10,2};
        arr = mergeSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }
}
